home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 19 / Mac Magazin and MacEasy Magazine CD - Issue 19.iso / Musik & Kunst / Ear Workout 2.1 / source code / complete_window.cp next >
Text File  |  1995-12-03  |  10KB  |  342 lines

  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <OSUtils.h>
  6. #include <QuickDraw.h>
  7. #include <Sound.h>
  8.  
  9. #define NEED_MAC_STUFF 1
  10.  
  11. #include "Ninkasi:C++ util:generic.h"
  12. #include "Ninkasi:C++ util:complete_window.h"
  13.  
  14. void copy_pascal_string(Str255,Str255);
  15. class complete_window;
  16. void dummy_den_mother(complete_window *x);
  17.  
  18. complete_window::complete_window(
  19.     int dlog_resource_id,
  20.         Str255 the_param_text0,
  21.         Str255 the_param_text1,
  22.         Str255 the_param_text2,
  23.         Str255 the_param_text3,
  24.         DEN_MOTHER_T *the_den_mother)
  25.         {
  26.           copy_pascal_string(param_text0,the_param_text0);
  27.           copy_pascal_string(param_text1,the_param_text1);
  28.           copy_pascal_string(param_text2,the_param_text2);
  29.           copy_pascal_string(param_text3,the_param_text3);
  30.           is_valid = 0;
  31.           resource_error = 0;
  32.           is_drawn = 0;
  33.           has_den_mother = VALID_POINTER(the_den_mother);
  34.           if (!has_den_mother)
  35.             den_mother = &dummy_den_mother;  //--make extra sure we don't mess up
  36.           else
  37.             den_mother = the_den_mother;
  38.           ParamText(param_text0,param_text1,param_text2,param_text3);
  39.           the_window = GetNewDialog(
  40.               (short) dlog_resource_id,
  41.               (Ptr) 0,            // allocate non-relocatable one for me
  42.               (WindowPtr) -1            // in front
  43.               );
  44.           if (!VALID_POINTER(the_window)) {
  45.             resource_error = ResError();
  46.           }
  47.           else {
  48.             is_valid = 1;
  49.             is_drawn = 1; // GetNewDialog draws it for me
  50.             whassup = complete_window_created;
  51.             if (VALID_POINTER(den_mother))
  52.               (*den_mother)(this);
  53.           }
  54.         }
  55.  
  56. complete_window::~complete_window()
  57.     {
  58.       whassup = complete_window_erase;
  59.           if (is_valid && VALID_POINTER(den_mother))
  60.             (*den_mother)(this);
  61.           if (VALID_POINTER(the_window))
  62.             DisposDialog(the_window);
  63.     }
  64.  
  65.     void
  66. dummy_den_mother(complete_window *x)
  67.     {
  68.     }
  69.  
  70.     // This is a routine to help with keeping your part numbers straight in
  71.     // DITLs.  The string "decoder" looks like, e.g.:
  72.     //    check_boxes/0:5,ok_button,radio_buttons/8
  73.     // which defines part numbers 1-6 as check_boxes 0 through 5,
  74.     // part number 7 as ok_button, etc.
  75.     // "Decoder" is a Pascal string, just as read in from an 'STR ' resource using
  76.     //        GetString.
  77.     // If 255 characters isn't enough for you, then instead you can use a string
  78.     //   consisting of a zero length byte, then the string, then a C-style null
  79.     //   terminator.
  80.     // "Nifty_name" is a C string.
  81.     // Returns -999 in *nifty_index if there's an error.
  82.     void
  83. part_number_to_nifty_label(
  84.     char *nifty_name,
  85.     int *nifty_index,
  86.     char *decoder,
  87.     int part_num)
  88.     {
  89.       int current_part_num,decoder_len,current_place,slash,comma,colon,
  90.           have_slash,have_colon,first_nifty_index,last_nifty_index,
  91.           n_nifty_indices;
  92.       current_part_num = 0;
  93.       decoder_len = * (unsigned char *) decoder;
  94.       if (decoder_len==0) decoder_len=strlen(decoder+1);
  95.       current_place = 1; // index into decoder
  96.       while (current_place<=decoder_len) {
  97.         comma = find_char_in_string(decoder,current_place,decoder_len,',',1);
  98.         slash = find_char_in_string(decoder,current_place,comma-1,'/',1);
  99.         have_slash = (slash<=comma-1);
  100.         if (!have_slash) {
  101.           first_nifty_index = 1;
  102.           last_nifty_index = 1;
  103.         }
  104.         else {
  105.           colon = find_char_in_string(decoder,slash+1,comma-1,':',1);
  106.           have_colon = (colon<=comma-1);
  107.           if (!have_colon) {
  108.             first_nifty_index = 1;
  109.             last_nifty_index = substring_to_integer(decoder,slash+1,comma-1);
  110.           }
  111.           else {
  112.             first_nifty_index = substring_to_integer(decoder,slash+1,colon-1);
  113.             last_nifty_index  = substring_to_integer(decoder,colon+1,comma-1);
  114.           }
  115.         }
  116.         n_nifty_indices = last_nifty_index - first_nifty_index + 1;
  117.         if (current_part_num + n_nifty_indices - 1 >= part_num) {
  118.           strncpy(nifty_name,decoder+current_place,slash-current_place);
  119.           nifty_name[slash-current_place] = '\0';
  120.           *nifty_index = first_nifty_index+part_num-current_part_num;
  121.           return;
  122.         }
  123.         current_place = comma+1;
  124.         current_part_num += n_nifty_indices;
  125.       }
  126.       *nifty_index = -999;
  127.     }
  128.  
  129.     int
  130. nifty_label_to_part_number(char *nifty_name,int nifty_index,char *decoder)
  131.     {
  132.       int current_part_num,decoder_len,current_place,slash,comma,colon,
  133.           have_slash,have_colon,first_nifty_index,last_nifty_index,
  134.           n_nifty_indices;
  135.       current_part_num = 1;
  136.       decoder_len = * (unsigned char *) decoder;
  137.       if (decoder_len==0) decoder_len=strlen(decoder+1);
  138.       current_place = 1; // index into decoder
  139.       while (current_place<=decoder_len) {
  140.         comma = find_char_in_string(decoder,current_place,decoder_len,',',1);
  141.         slash = find_char_in_string(decoder,current_place,comma-1,'/',1);
  142.         have_slash = (slash<=comma-1);
  143.         if (!have_slash) {
  144.           first_nifty_index = 1;
  145.           last_nifty_index = 1;
  146.         }
  147.         else {
  148.           colon = find_char_in_string(decoder,slash+1,comma-1,':',1);
  149.           have_colon = (colon<=comma-1);
  150.           if (!have_colon) {
  151.             first_nifty_index = 1;
  152.             last_nifty_index = substring_to_integer(decoder,slash+1,comma-1);
  153.           }
  154.           else {
  155.             first_nifty_index = substring_to_integer(decoder,slash+1,colon-1);
  156.             last_nifty_index  = substring_to_integer(decoder,colon+1,comma-1);
  157.           }
  158.         }
  159.         n_nifty_indices = last_nifty_index - first_nifty_index + 1;
  160.         if (slash-current_place==strlen(nifty_name)
  161.              && strncmp(nifty_name,decoder+current_place,slash-current_place)==0) {
  162.           return current_part_num + nifty_index - first_nifty_index;
  163.         }
  164.         current_place = comma+1;
  165.         current_part_num += n_nifty_indices;
  166.       }
  167.       return -999;
  168.     }
  169.  
  170.  
  171.     int
  172. substring_to_integer(char *string,int first,int last)
  173.     {
  174.       int i;
  175.       char s[30];
  176.       if (last-first>29 || last-first<0) return 0;
  177.       for (i=first; i<=last; i++) {
  178.         s[i-first] = string[i];
  179.       }
  180.       s[last-first+1] = '\0';
  181.       return atoi(s);
  182.     }
  183.  
  184.     // if_not_found = 1 says return last+1 if not found
  185.     // if_not_found = 2 says return -1 if not found
  186.     int
  187. find_char_in_string(char *the_string,int first,int last,char the_char,int if_not_found)
  188.     {
  189.       int i;
  190.       for (i=first; i<=last; i++) {
  191.         if (the_string[i]==the_char) return i;
  192.       }
  193.       switch(if_not_found) {
  194.         case 1: return last+1;
  195.         case 2: return -1;
  196.         default: return -999;
  197.       }
  198.     }
  199.     
  200.     void
  201. set_ctl_by_nifty_label(WindowPtr w,char *nifty_label,int nifty_index,
  202.             char *decoder_string,int value)
  203.     {
  204.         int p;
  205.         short tt;
  206.         Handle hh;
  207.         Rect rr;
  208.         p = nifty_label_to_part_number(
  209.                 nifty_label,nifty_index,decoder_string);
  210.         if (p != -999) {
  211.           GetDItem(w,(short) p,&tt,&hh,&rr);
  212.           if (VALID_HANDLE(hh))
  213.             SetCtlValue((ControlRecord **) hh,value);
  214.         }
  215.     }
  216.  
  217.     void
  218. get_rect_by_nifty_label(WindowPtr w,char *nifty_label,int nifty_index,
  219.             char *decoder_string,Rect *rr)
  220.     {
  221.         int p;
  222.         short tt;
  223.         Handle hh;
  224.         p = nifty_label_to_part_number(
  225.                 nifty_label,nifty_index,decoder_string);
  226.         if (p != -999)
  227.           GetDItem(w,(short) p,&tt,&hh,rr);
  228.         else
  229.           SetRect(rr,(short) 9000,(short) 9000,(short) 9100,(short) 9100);
  230.     }
  231.  
  232.     void
  233. get_item_by_nifty_label(WindowPtr w,char *nifty_label,int nifty_index,
  234.             char *decoder_string,Handle *h)
  235.     {
  236.         int p;
  237.         short tt;
  238.         Rect rr;
  239.         p = nifty_label_to_part_number(
  240.                 nifty_label,nifty_index,decoder_string);
  241.         if (p != -999)
  242.           GetDItem(w,(short) p,&tt,h,&rr);
  243.         else
  244.           *h = (Handle) 0;
  245.     }
  246.  
  247.     void
  248. show_ctl_by_nifty_label(WindowPtr w,char *nifty_label,int nifty_index,
  249.             char *decoder_string)
  250.     {
  251.         int p;
  252.         short tt;
  253.         Handle hh;
  254.         Rect rr;
  255.         p = nifty_label_to_part_number(
  256.                 nifty_label,nifty_index,decoder_string);
  257.         if (p != -999) {
  258.           ShowDItem(w,(short) p);
  259.         }
  260.     }
  261.  
  262.     void
  263. hide_ctl_by_nifty_label(WindowPtr w,char *nifty_label,int nifty_index,
  264.             char *decoder_string)
  265.     {
  266.         int p;
  267.         short tt;
  268.         Handle hh;
  269.         Rect rr;
  270.         p = nifty_label_to_part_number(
  271.                 nifty_label,nifty_index,decoder_string);
  272.         if (p != -999) {
  273.           HideDItem(w,(short) p);
  274.         }
  275.     }
  276.  
  277.     void
  278. activate_ctl_by_nifty_label(WindowPtr w,char *nifty_label,int nifty_index,
  279.             char *decoder_string)
  280.     {
  281.         int p;
  282.         short tt;
  283.         Handle hh;
  284.         Rect rr;
  285.         get_item_by_nifty_label(w,nifty_label,nifty_index,
  286.             decoder_string,&hh);
  287.         if (VALID_HANDLE(hh)) {
  288.           HiliteControl((ControlHandle) hh,(short) 0);
  289.         }
  290.     }
  291.  
  292.     void
  293. deactivate_ctl_by_nifty_label(WindowPtr w,char *nifty_label,int nifty_index,
  294.             char *decoder_string)
  295.     {
  296.         int p;
  297.         short tt;
  298.         Handle hh;
  299.         Rect rr;
  300.         get_item_by_nifty_label(w,nifty_label,nifty_index,
  301.             decoder_string,&hh);
  302.         if (VALID_HANDLE(hh)) {
  303.           HiliteControl((ControlHandle) hh,(short) 255);
  304.         }
  305.     }
  306.  
  307.     void
  308. refresh_text(complete_window *my_complete_window,
  309.             char *nifty_label,int nifty_index,char *decoder_string)
  310.     {
  311.           Handle h;
  312.           Str255 s;
  313.           get_item_by_nifty_label(my_complete_window->the_window,
  314.               nifty_label,nifty_index,decoder_string,&h);
  315.           if (VALID_HANDLE(h)) {
  316.             GetIText(h,s);
  317.             SetIText(h,s);
  318.           }
  319.     }
  320.  
  321.     void
  322. message_to_den_mother(complete_window *the_complete_window,void **data_h,
  323.             void *data_p,int data_i,char *data_v)
  324.     {
  325.       if (!VALID_POINTER(the_complete_window)
  326.            || !the_complete_window->is_valid
  327.            || !VALID_POINTER(the_complete_window->den_mother))
  328.         return;
  329.  
  330.       the_complete_window->whassup = complete_window_action;
  331.       the_complete_window->the_event = (EventRecord *) 0;
  332.       the_complete_window->part_code = -1;
  333.       the_complete_window->part_number = -1;
  334.       the_complete_window->data_handle = data_h;
  335.       the_complete_window->data_pointer = data_p;
  336.       the_complete_window->data_int = data_i;
  337.       the_complete_window->data_verb = data_v;
  338.       (*(the_complete_window->den_mother))(the_complete_window);
  339.     }
  340.  
  341.  
  342.